/**
 * Created with IntelliJ IDEA.
 * User: Никита
 * Date: 03.08.13
 * Time: 20:28
 * To change this template use File | Settings | File Templates.
 */
$(function () {
    $("div#content").on('click', '.contentBlock.candidate', onBlockClick).
        on('dblclick', '.contentBlock.mode_committed', onBlockClick).
        on('focusout', '.contentBlock.editableContentWrapper', onEditableFocusOut).
        on("mousedown", '.contentBlock.editableContentWrapper', onTextBoxResizeMouseDown).
        on('mouseup', '.contentBlock.editableContentWrapper', onTextBoxChangeSizeFinish).
        on('mouseover', '.contentBlock.editableContentWrapper', onEditableBlockMouseOver).
        on('mouseout', ".contentBlock.editableContentWrapper", onEditableBlockMouseOut).
        on('click', ".control_panel .cmd-ok", onEditableCmdCommit);
});
function onBlockClick() {
    new TextContent(this).edit();
}
function onEditableBlockMouseOver() {
    new TextContent(this).showControls();
}
function onEditableBlockMouseOut() {
    new TextContent(this).hideControls();
}
function onTextBoxResizeMouseDown() {
    var $textarea = $(this).find('.hasText').first();
    var $control = $(this).find('.control_panel');
    if (!$textarea.data('init_width')) {
        $textarea.data('init_width', $textarea.width());
        $control.css({
            right: 'auto',
            left: $control.position().left + 'px'
        })
    }
}
function onTextBoxChangeSizeFinish() {
    var $textarea = $(this).find('.hasText').first();
    var offset = $textarea.data('init_width') - $textarea.outerWidth();
    if (offset != 0) {
        var $control = $(this).find('.control_panel');
        var newLeft = $textarea.outerWidth();
        $control.css({
            right: 'auto',
            left: newLeft + 'px'
        })
    }
    $textarea.data('wth', $textarea.outerWidth());
}
function onEditableCmdCommit() {
    new TextContent(getParentContentBlock(this)).commit();
}
function TextContent(contentBlock) {
    this.block = $(contentBlock).filter('.contentBlock').first();
    if (this.block.size() == 0)
        throw Error('DOM element is not content block');

    this.commit = textContentNs.commit;

    this.delete = textContentNs.delete;

    this.edit = textContentNs.edit;

    this.showControls = textContentNs.showControls;

    this.hideControls = textContentNs.hideControls;

    this.isEmpty = textContentNs.isEmpty;

}
var textContentNs = {
    isEmpty: function () {
        return !$.trim(getTextFromBlock(this.block))
    },
    hideControls: function () {
        getBlockControl(this.block).hide();
    },
    showControls: function () {
        getBlockControl(this.block).show();
    },
    edit: function () {
        var $textContainer = getTextContainer(this.block);
        var text = getTextFromBlock($textContainer);
        var $editableBlockSample = $('#samples').find('.contentBlock.mode_edit').first();
        if ($editableBlockSample.size() == 0) {
            throw Error('Sample for editing was not found');
        }
        var $editableBlock = $editableBlockSample.clone();
        var $editableBlockTextContainer = getTextContainer($editableBlock);
        if ($editableBlockTextContainer == null) {
            throw Error('Content block does not contain text container');
        }
        $editableBlockTextContainer.text(text);
        this.block.replaceWith($editableBlock);
        $editableBlockTextContainer.focus();
        this.block = $editableBlock;

    },
    commit: function () {
        var $textContainer = getTextContainer(this.block);
        var text = getTextFromBlock($textContainer);
        if ($.trim(text)) {
            var $committedBlockSample = $('#samples').find('.contentBlock.mode_committed').first();
            if ($committedBlockSample.size() == 0) {
                throw Error('Content block for commit was not found');
            }
            var $committedBlock = $committedBlockSample.clone();
            var $targetTextContainer = getTextContainer($committedBlock);
            if ($targetTextContainer) {
                $targetTextContainer.text(text);
            } else {
                throw Error('Content block does not contain text container');
            }
            this.block.replaceWith($committedBlock);
            this.block = $committedBlock;
        } else {
            this.delete();
        }
    },
    delete: function () {
        this.block.detach();
    }
}

function getTextContainer(parentBlock) {
    if (parentBlock == null || $(parentBlock).size() == 0)
        return null;
    var textBlock;
    parentBlock = $(parentBlock);
    if (parentBlock.hasClass('hasText'))
        textBlock = parentBlock;
    else
        textBlock = parentBlock.find('.hasText').first();
    return textBlock.size() == 1 ? textBlock : null;
}

function getTextFromBlock(parentBlock) {
    var container = getTextContainer(parentBlock);
    return container == null ? null : container.text();
}

function getBlockControl(block) {
    return $(block).find('.control_panel');
}
function getParentContentBlock(childBlock) {
    return $(childBlock).closest('.contentBlock');
}